const {Model, DataTypes} = require('sequelize');
const sequelize = require('../config/db');

class Goods extends Model {}

Goods.init({
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },

    type: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    cover: {
        type: DataTypes.STRING,
        allowNull: false
    },
    content: {
        type: DataTypes.TEXT('long'),
        allowNull: false
    },
    history_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
            model: 'history',
            key: 'id'
        }
    }
}, {
    sequelize,
    modelName: 'goods',
    tableName: 'goods',
    timestamps: false
});

// 定义数据模型间的关联时，确保先导入所需的关联模型
const RelatedFigures = require('./related_figures');
const RelatedEvents = require('./related_events');

// 设置与历史人物的一对多关系
Goods.hasMany(RelatedFigures, {
    foreignKey: 'goods_id',
    as: 'historicalFigures',
    onDelete: 'CASCADE'
});
RelatedFigures.belongsTo(Goods, {
    foreignKey: 'goods_id'
});

// 设置与历史事件的一对多关系
Goods.hasMany(RelatedEvents, {
    foreignKey: 'goods_id',
    as: 'historicalEvents',
    onDelete: 'CASCADE'
});
RelatedEvents.belongsTo(Goods, {
    foreignKey: 'goods_id'
});

module.exports = Goods;

